package com.example.marksixanim

import android.animation.Animator
import android.content.Context
import android.graphics.drawable.AnimationDrawable
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.OvershootInterpolator
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout

/**
 * @author robert
 * @date 2017/12/22
 */
class MarkSixView : FrameLayout {
    companion object {
        val TAG: String = MarkSixView::class.java.simpleName
    }

    private lateinit var mBgAnim: ImageView
    private lateinit var mLeftImage: ImageView
    private lateinit var mRighImage: ImageView
    private lateinit var mLlScreen: LinearLayout
    private lateinit var mBgGray: FrameLayout
    private lateinit var mLlBallList: LinearLayout
    private val mBallViews: MutableList<BallView> = ArrayList(7)
    /**
     * 是否拉开帷幕
     */
    var isScreenOn = false
    /**
     * 是否有结果
     */
    var isHasResult = false

    var callBack: CallBack? = null

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        mBgAnim = findViewById(R.id.bg_anim)
        mLeftImage = findViewById(R.id.iv_screen_left)
        mRighImage = findViewById(R.id.iv_screen_right)
        mLlScreen = findViewById(R.id.ll_screen)
        mBgGray = findViewById(R.id.bg_gray)
        mLlBallList = findViewById(R.id.ll_ball_list)
        mBallViews.add(findViewById(R.id.ball1))
        mBallViews.add(findViewById(R.id.ball2))
        mBallViews.add(findViewById(R.id.ball3))
        mBallViews.add(findViewById(R.id.ball4))
        mBallViews.add(findViewById(R.id.ball5))
        mBallViews.add(findViewById(R.id.ball6))
        mBallViews.add(findViewById(R.id.ball7))
        Log.e(TAG, "onAttachedToWindow")
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        Log.e(TAG, "onDetachedFromWindow")
    }

    /**
     * 背景开始旋转
     */
    private fun startBgRotate() {
        if (mBgAnim.drawable is AnimationDrawable) {
            val background = mBgAnim.drawable as AnimationDrawable
            background.start()
        }
    }

    /**
     * 开始动画
     */
    fun startScreenExit() {
        if (isHasResult) {
            return
        }
        mBgGray.visibility = View.GONE
        mLlScreen.visibility = View.VISIBLE
        mBgAnim.visibility = View.VISIBLE
        startBgRotate()
        mLeftImage.animate()
                .setDuration(1000)
                .translationXBy(-mLeftImage.width.toFloat())
                .setListener(object : SimpleAnimatorListener() {
                    override fun onAnimationEnd(animation: Animator) {
                        mLlScreen.visibility = View.GONE
                    }
                })
        mRighImage.animate()
                .setDuration(1000)
                .translationXBy(mRighImage.width.toFloat())
        isScreenOn = true
    }

    /**
     *  重置动画
     */
    fun reset() {
        //重置帷幕
        mLlScreen.visibility = View.VISIBLE
        mLeftImage.translationX = 0f
        mRighImage.translationX = 0f
        //重置背景动画
        mBgAnim.visibility = View.VISIBLE
        mBgAnim.setImageResource(R.drawable.bg_mark_six_anim)

        //重置开奖
        mBgGray.visibility = View.GONE
        mLlBallList.visibility = View.GONE

        isHasResult = false
        isScreenOn = false
    }

    /**
     * 得到结果设置进来
     */
    fun openResult(ballList: List<BallInfo>) {
        if (isScreenOn) {
            if (ballList.size > 7) {
                throw RuntimeException("六合彩球号超标")
            }
            ballList.forEachIndexed { index, ballInfo ->
                mBallViews[index].setAnimal(ballInfo.getAnimalDrawable())
                mBallViews[index].setColor(ballInfo.getColorDrawable())
                mBallViews[index].setNumber(ballInfo.getNumberDrawable())
                mBallViews[index].scaleX = 0f
                mBallViews[index].scaleY = 0f
            }
            mBgAnim.setImageResource(R.drawable.bg_mark_six_stop_anim)
            mBgGray.alpha = 0.2f
            mBgGray.visibility = View.VISIBLE
            mBgGray.animate()
                    .setDuration(500)
                    .alpha(1.0f)
                    .setListener(object : SimpleAnimatorListener() {
                        override fun onAnimationEnd(animation: Animator) {
                            startResult()
                        }
                    })
        }
    }

    /**
     * 设置一个接一个的球
     */
    fun startResult() {
        mLlBallList.visibility = View.VISIBLE
        mBallViews.forEachIndexed { index, ballView ->
            if (index > 0) {
                val delay = (index * 200).toLong()
                ballView.animate()
                        .setDuration(800)
                        .setStartDelay(delay)
                        .setInterpolator(OvershootInterpolator())
                        .scaleX(1f)
                        .scaleY(1f)

            } else if (index == mBallViews.size - 1) {
                ballView.animate()
                        .setDuration(800)
                        .setStartDelay((index * 200).toLong())
                        .setInterpolator(OvershootInterpolator())
                        .scaleX(1f)
                        .scaleY(1f)
                        .setListener(object : SimpleAnimatorListener() {
                            override fun onAnimationEnd(animation: Animator) {
                                callBack?.onFinish()
                            }
                        })
            } else {
                ballView.animate()
                        .setDuration(800)
                        .setInterpolator(OvershootInterpolator())
                        .scaleX(1f)
                        .scaleY(1f)
            }
        }
    }


    interface CallBack {
        fun onFinish()
    }

}